import json
from urllib.parse import urlencode
from conf import *
import re
import requests
from multiprocessing import Pool
from requests import RequestException

def get_douban(douban_start,douban_limit):
    data = {
        'type': 'movie',
        'tag': '豆瓣高分',
        'sort': 'recommend',
        'page_limit': limit,
        'page_start': start
    }
    url = "https://movie.douban.com/j/search_subjects?" + urlencode(data)
    print(url)
    header = {
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
        'Host' :'movie.douban.com',
        'Referer' :'https://movie.douban.com/explore'
    }

    try:
        response = requests.get(url,headers = header)
        if response.status_code == 200 :
            return response.text
        return None
    except RequestException:
        print("url索引错误")
        return None

def parse_douban(response):
    data = json.loads(response)
    items = data.get("subjects")
    for item in items:
        print(item)

def write_file_douban(content):
    with open('doubanmovies.txt','a',encoding='utf-8') as f:
        f.write(json.dumps(content,ensure_ascii=False) + '\n')
        f.close()

def main() :
    response = get_douban(start,limit)
    items = parse_douban(response)



if __name__ == '__main__':
    main()